/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.customer.core.domain.partner.action.PartnerConverter;
import info.peigen.hotpot.business.customer.core.repository.entity.PartnerEntity;
import info.peigen.hotpot.business.customer.facade.PartnerQueryServiceFacade;
import info.peigen.hotpot.business.customer.facade.info.partner.PartnerInfo;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerPageQueryOrder;
import info.peigen.hotpot.business.customer.facade.order.partner.PartnerQueryOrder;
import info.peigen.hotpot.business.customer.facade.result.partner.PartnerQueryPageResult;
import info.peigen.hotpot.business.customer.facade.result.partner.PartnerQueryResult;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceQuery;
import info.peigen.hotpot.common.service.base.ServiceQueryContext;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import org.beetl.sql.core.page.DefaultPageResult;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <b>(PartnerQueryService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/7/29
 */
@Component
public class PartnerQueryService extends AbstractPartnerQueryService implements PartnerQueryServiceFacade {
    @Override
    public PartnerQueryResult queryPartnerInfo(PartnerQueryOrder order) {
        ServiceQueryContext serviceContext = ServiceQueryContext.builder().order(order).result(new PartnerQueryResult()).build();
        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                PartnerEntity entity = partnerRepository.getMapper().findByPartnerId(order.getPartnerId());
                Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "查询[Partner]失败![partnerId=" + order.getPartnerId() + "]"));
                serviceContext.setEntity(entity);
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                PartnerEntity entity = (PartnerEntity) serviceContext.getEntity();
                PartnerInfo   info   = PartnerConverter.convertInfo(entity);
                ((PartnerQueryResult) serviceContext.getResult()).fromSuccess(info);
            }
        });
        return (PartnerQueryResult) serviceContext.getResult();
    }

    @Override
    public PartnerQueryPageResult queryPartnerPage(PartnerPageQueryOrder order) {
        ServiceQueryContext serviceContext = new ServiceQueryContext().setOrder(order).setResult(new PartnerQueryPageResult());

        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                // 1. 查 Entity
                PageResult<PartnerEntity> partnerEntityPage = partnerRepository.getMapper().createLambdaQuery()
                        .andEq(PartnerEntity::getStatus, Query.filterEmpty(order.getStatus()))
                        // todo beetlsql添加了@LogicDelete后，在排序就会出问题。sql语句成会这样：SELECT * FROM `partner` WHERE `status` = ? AND enable!=0ORDER BY `create_time` DESC limit 0 , 10
                        .desc(PartnerEntity::getCreateTime)
                        .page(order.getPageNum(), order.getPageSize());
                List<PartnerEntity> partnerEntities = partnerEntityPage.getList();
                // 2. 转换 Info
                List<PartnerInfo> partnerInfos = new ArrayList<>();
                if (!CollUtil.isEmpty(partnerEntities)) {
                    partnerInfos = PartnerConverter.convertInfo(partnerEntities);
                }

                org.beetl.sql.core.page.PageResult<PartnerInfo> partnerInfoPage = BeanUtil.copyProperties(partnerEntityPage, DefaultPageResult.class, "list");
                partnerInfoPage.setList(partnerInfos);
                ((PartnerQueryPageResult) serviceContext.getResult()).fromSuccess(partnerInfoPage.getList());
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {

            }

        });
        return (PartnerQueryPageResult) serviceContext.getResult();
    }
}